package com.hjy.web.controller;

import com.google.common.collect.Maps;
import com.hjy.beans.PageQuery;
import com.hjy.beans.PageResult;
import com.hjy.common.JsonData;
import com.hjy.param.UserParam;
import com.hjy.pojo.SysUser;
import com.hjy.service.SysRoleService;
import com.hjy.service.SysTreeService;
import com.hjy.service.SysUserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/sys/user")
public class SysUserController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private SysTreeService sysTreeService;
    @Resource
    private SysRoleService sysRoleService;

    @RequestMapping("/save.json")
    @ResponseBody
    public JsonData saveUser(UserParam param,HttpServletRequest request) {
        sysUserService.save(param,request);
        return JsonData.success();
    }

    @RequestMapping("/get")
    @ResponseBody
    public Object saveUser() {

        return sysUserService.findSysUser("admin");
    }

    @RequestMapping("/update.json")
    @ResponseBody
    public JsonData updateUser(UserParam param,HttpServletRequest request) {
        sysUserService.update(param,request);
        return JsonData.success();
    }

    @RequestMapping("/page.json")
    @ResponseBody
    public JsonData page(@RequestParam("deptId") int deptId, PageQuery pageQuery) {
        PageResult<SysUser> result = sysUserService.getPageByDeptId(deptId, pageQuery);
        return JsonData.success(result);
    }

    @RequestMapping("/acls.json")
    @ResponseBody
    public JsonData acls(@RequestParam("userId") int userId,HttpServletRequest request) {
        Map<String, Object> map = Maps.newHashMap();
        map.put("acls", sysTreeService.userAclTree(userId,request));
        map.put("roles", sysRoleService.getRoleListByUserId(userId));
        return JsonData.success(map);
    }
}
